<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class WagesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("admin.wages.index");
    }
    public function getwages(Request $request){
        $input = $request->input();
        $start = $input['page']*$input['limit']-$input['limit'];
        $end = $input['page']*$input['limit'];
        $data = [
            'code'=>0,
            'msg'=>'',
            'count'=>DB::table("wages")->where('type',0)->count(),
            'data'=>DB::table("wages")->where('type',0)->offset($start)->limit($end)->get()
        ];
        return $data;
    }
    public function shenhe(Request $request){
        $input = $request->except('_token');
        $wage_data = DB::table("wages")->where("id",$input['id'])->first();
        if($wage_data->type == 1){
            return "该工资已经审核";
        }
        $date = date("Y-m-d");
        $re = DB::table("wages")->where("id",$input['id'])->update(['type'=>1]);
        if($re){
            $time_date = DB::table("times")->where("str_time",$date)->first();
            if($time_date){
                DB::table("times")->where('id',$time_date->id)->increment('money_z',$wage_data->money);
                DB::table("times")->where('id',$time_date->id)->increment('money_w',$wage_data->money);
            }else{
                DB::table("times")->insert(['str_time'=>$date,'money_z'=>$wage_data->money,'money_w'=>$wage_data->money]);
            }
            return 1;
        }else{
            return "审核失败";
        }
    }
    public function shenhe_all(Request $request){
        $input = $request->except('_token');
        if(!$input){
            return "未选择数据";
        }
        foreach ($input['data'] as $v){
            $wage_data = DB::table("wages")->where("id",$v['id'])->first();
            if($wage_data->type == 1){
                return "该工资已经审核";
            }
            $date = date("Y-m-d");
            $re = DB::table("wages")->where("id",$v['id'])->update(['type'=>1]);
            if($re){
                $time_date = DB::table("times")->where("str_time",$date)->first();
                if($time_date){
                    DB::table("times")->where('id',$time_date->id)->increment('money_z',$wage_data->money);
                    DB::table("times")->where('id',$time_date->id)->increment('money_w',$wage_data->money);
                }else{
                    DB::table("times")->insert(['str_time'=>$date,'money_z'=>$wage_data->money,'money_w'=>$wage_data->money]);
                }
            }
        }
        return 1;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.wages.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->except('_token');
        $re = DB::table("wages")->insert($input);
        if($re){
            return view("welcome",['id'=>1,'msg'=>'添加成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = DB::table("wages")->where("id",$id)->first();
        return view("admin.wages.edit",['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->except('_token','_method');
        $re = DB::table("wages")->where("id",$id)->update($input);
        if($re){
            return view("welcome",['id'=>1,'msg'=>'编辑成功']);
        }else{
            return view("welcome",['id'=>2,'msg'=>'编辑失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
